package com.pactera.activity;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URLEncoder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.test.R;
import com.pactera.interFace.IFailCallback;
import com.pactera.interFace.ISuccessCallback;
import com.pactera.utils.HttpUtil;
import com.pactera.widget.NetImageView;

/**
 * activity����
 * 
 * @author David
 * 
 */
public class BaseActivity extends Activity implements IFailCallback,
		ISuccessCallback {

	public static String BASE_URL = "http://m.hao24.cn/mobile/";

	public static final String GET_GOODSCLASS = "get_goodsClass.php";

	private HttpURLConnection urlConnection = null;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Toast.makeText(BaseActivity.this, "the error code is " + msg.what,
					500).show();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		NetImageView image = (NetImageView) findViewById(R.id.image);
		image.setDownloadurl("http://t3.baidu.com/it/u=926877036,3373539525&fm=24&gp=0.jpg");
		requestUrl(BASE_URL + GET_GOODSCLASS);
	}

	protected void requestUrl(String urlString) {
		String result = null;
		try {
			urlConnection = HttpUtil.getHttpURLConnection(urlString, "GET",
					"application/json", "");
			urlConnection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded; charset=utf-8");
			urlString = URLEncoder.encode(urlString, "UTF-8");
			urlConnection.setConnectTimeout(20000);
			urlConnection.connect();
			int response = urlConnection.getResponseCode();

			if (response == 200) {
				InputStream in = urlConnection.getInputStream();// .openInputStream();
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				byte[] buff = new byte[1024];
				int rc = 0;
				while ((rc = in.read(buff, 0, 1024)) != -1) {
					baos.write(buff, 0, rc);
				}
				byte[] content = baos.toByteArray();
				if (content.length > 0) {
					result = new String(content, "UTF-8");
				}
				in.close();
				in = null;
				baos.close();
				baos = null;
			} else {
				Message message = new Message();
				message.what = response;
				handler.sendMessage(message);
			}

		} catch (Exception e) {
			Log.e("BaseActivity", e.toString());
			result = null;
		} finally {
			if (urlConnection != null) {
				// .close();
				urlConnection.disconnect();
				urlConnection = null;
			}
		}
		sucess(result);
	}

	@Override
	public void sucess(String resultString) {
//		Log.v("BaseActivity", resultString);
	}

	@Override
	public void failed() {
		// TODO Auto-generated method stub
	}
}
